/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("DealWith",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using XenFin.Read.System.IServices;
using SqlSugar;
using Microsoft.AspNetCore.Authorization;
using SkiaSharp;
using VOL.Core.DBManager;
using XenFin.Read.Entity.DomainModels;

namespace XenFin.Read.System.Controllers
{
    public partial class DealWithController
    {
        private readonly IDealWithService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISqlSugarClient _db;

        [ActivatorUtilitiesConstructor]
        public DealWithController(
            IDealWithService service,
            IHttpContextAccessor httpContextAccessor,
            ISqlSugarClient db
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            _db = db;
        }

        /// <summary>
        /// 更改实付金额
        /// </summary>
        /// <param name="dealWith"></param>
        /// <returns></returns>
        [HttpPost,Route("UpdatePrice"),AllowAnonymous]
        public int UpdatePrice([FromBody]DealWith dealWith)
        {
            //实付金额不能大于成交金额
            if (dealWith.ActualPrice > dealWith.DealWithPrice)
            {
                return -1;
            }
            dealWith.ResiduePrice = dealWith.DealWithPrice - dealWith.ActualPrice;
            return _db.Updateable(dealWith).ExecuteCommand();
        }

        [HttpGet, Route("GetSelectOne"), AllowAnonymous]
        public IActionResult GetSelectOne()
        {
            // 使用 SQLSugar 查询数据
            var result = _db.Queryable<FinancialCategory>()
                            .Select(fc => new {
                                key = fc.FinancialCategoryId,
                                value = fc.CategoryName
                            })
                            .ToList();

            // 返回查询结果
            return Ok(result);
        }

        [HttpGet, Route("ChangeState"), AllowAnonymous]
        public int ChangeState(int state,string DealWithId)
        {
            var result = _db.Queryable<DealWith>().Where(it => it.DealWithId == DealWithId).FirstOrDefault();
            result.PayReceState = state;

            if(result.PayReceState == 2)
            {
                var prefix = DateTime.Now.ToString("yyyyMMdd");
                var lastCategory = _db.Queryable<Financial>()
                                       .OrderByDescending(it => it.FinancialAccountId)
                                       .FirstOrDefault();
                int number = 0;
                if (lastCategory != null)
                {
                    number = int.Parse(lastCategory.FinancialAccountId.Substring(lastCategory.FinancialAccountId.Length - 4)) + 1;
                }
                else
                {
                    number = 1;
                }

                Financial financial = new Financial()
                {
                    FinancialAccountId = $"{"FIN" + prefix}{number.ToString().PadLeft(4, '0')}",
                    ReceivableId = null,
                    DealWithId = result.DealWithId,
                    FinancialName = result.DealWithName,
                    CategoryName = result.Ordertype,
                    ReceiptPayMent = result.Payer,
                    PayableReceivable = "应付",
                    PaymentPrice = result.DealWithPrice,
                    PaymentTime = result.EndOrderTime,
                    OrderOrganization = "个人",

                };

                _db.Insertable(financial).ExecuteCommand();
            }

            return _db.Updateable(result).ExecuteCommand();


        }
    }
}
